package com.dianxin.smartpark.service.common.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.constant.ErrMsgEnum;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.Convert;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.chinatelecom.response.utils.StringUtils;
import com.dianxin.acl.api.acl.AclDeptDicRpcService;
import com.dianxin.acl.api.acl.AclFileRpcService;
import com.dianxin.acl.api.acl.AclUserRpcService;
import com.dianxin.acl.api.dto.AclDeptDicDTO;
import com.dianxin.acl.api.dto.SsoUserDTO;
import com.dianxin.acl.api.dto.SsoUserDeptDTO;
import com.dianxin.acl.api.dto.SysFileDTO;
import com.dianxin.acl.api.qo.AclDeptDicQO;
import com.dianxin.acl.api.qo.SsoDeptRpcQO;
import com.dianxin.acl.api.qo.SsoUserRpcQO;
import com.dianxin.acl.api.qo.SysFileQO;
import com.dianxin.common.mqtt.service.MqttService;
import com.dianxin.common.oss.properties.FileUploadResponse;
import com.dianxin.common.oss.util.MinioUtil;
import com.dianxin.device.api.device.SmartParkDeviceRpcService;
import com.dianxin.device.api.device.dto.SmartDeviceDTO;
import com.dianxin.device.api.device.qo.SmartDeviceRpcQO;
import com.dianxin.smartpark.dao.dataobject.common.SmartParkCard;
import com.dianxin.smartpark.dao.dataobject.common.SmartParkCardExample;
import com.dianxin.smartpark.dao.dataobject.common.SmartParkUserConfig;
import com.dianxin.smartpark.dao.dataobject.common.SmartParkUserConfigExample;
import com.dianxin.smartpark.dao.dataobject.park.SmartParkCar;
import com.dianxin.smartpark.dao.dataobject.park.SmartParkCarExample;
import com.dianxin.smartpark.dao.dataobject.visitor.SmartParkStrategyConfig;
import com.dianxin.smartpark.dao.dataobject.visitor.SmartParkStrategyConfigExample;
import com.dianxin.smartpark.dao.dataobject.visitor.SmartParkVisitor;
import com.dianxin.smartpark.dao.dataobject.visitor.SmartParkVisitorExample;
import com.dianxin.smartpark.dao.mapper.common.SmartParkCardMapper;
import com.dianxin.smartpark.dao.mapper.common.SmartParkUserConfigMapper;
import com.dianxin.smartpark.dao.mapper.park.SmartParkCarMapper;
import com.dianxin.smartpark.dao.mapper.visitor.SmartParkStrategyConfigMapper;
import com.dianxin.smartpark.dao.mapper.visitor.SmartParkVisitorMapper;
import com.dianxin.smartpark.dao.qo.common.SmartParkUserConfigQO;
import com.dianxin.smartpark.service.common.SmartParkDeviceAuthService;
import com.dianxin.smartpark.service.common.SmartParkUserConfigService;
import com.dianxin.smartpark.service.config.MqttOptType;
import com.dianxin.smartpark.service.config.MqttTopicType;
import com.dianxin.smartpark.service.face.FaceEngineService;
import com.dianxin.smartpark.service.vo.common.*;
import com.dianxin.smartpark.service.vo.park.SmartParkUserCarVO;
import org.apache.poi.ss.formula.functions.T;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 设备权限下发 获取处理
 * 
 * @author chenjiasong
 * @date 2021-08-10
 */
@Service
public class SmartParkDeviceAuthServiceImpl implements SmartParkDeviceAuthService
{
    private final static Logger logger = LoggerFactory.getLogger(SmartParkDeviceAuthServiceImpl.class);

    private static final Logger mqtt = LoggerFactory.getLogger("mqtt-send");
    @Resource
    private SmartParkUserConfigMapper smartParkUserConfigMapper;

    @Resource
    private SmartParkDeviceRpcService smartParkDeviceRpcService;

    @Resource
    private SmartParkCardMapper smartParkCardMapper;

    @Resource
    private MqttService mqttService;

    @Resource
    private AclUserRpcService aclUserRpcService;

    @Resource
    private AclDeptDicRpcService aclDeptDicRpcService;

    @Resource
    private SmartParkStrategyConfigMapper smartParkStrategyConfigMapper;

    @Resource
    private SmartParkCarMapper smartParkCarMapper;


    /**
     * 下发用户设备权限
     *
     * @param userId 用户id
     */
    @Override
    public void sendUserDeviceAuth(String userId, Integer opt, String cardNum) throws Exception {
        //下发设备权限
        //查出用户已有设备
        //根据设备code查询所有绑定人
        List<String> devices = getDeviceByUserId(userId);

        if(StringUtils.isEmpty(devices)){
            return;
        }
        String device = ArrayUtil.join(devices.toArray(),",");
        //查询员工凭证
        SmartParkUserConfigExample smartParkUserConfigExample = new SmartParkUserConfigExample();
        SmartParkUserConfigExample.Criteria criteria = smartParkUserConfigExample.createCriteria();
        criteria.andIsDeletedEqualTo(0);
        criteria.andAccessStartDateLessThanOrEqualTo(new Date());
        criteria.andAccessEndDateGreaterThanOrEqualTo(new Date());
        criteria.andUserIdEqualTo(Convert.toLong(userId));
        List<SmartParkUserConfig> passRecords = smartParkUserConfigMapper.selectByExample(smartParkUserConfigExample);
        if (StringUtils.isEmpty(passRecords)) {
            return;
        }
        SmartParkUserConfig smartParkUserConfig = passRecords.get(0);

        //人脸权限下发
        if(StringUtils.isEmpty(cardNum)){
            SmartParkUserTzVO smartParkUserTzVO = new SmartParkUserTzVO();
            if(StringUtils.isEmpty(smartParkUserConfig.getCharacteristic())){
                return;
            }
            if(smartParkUserConfig.getBeAuthorization() != 1){
                return;
            }
            smartParkUserTzVO.setUserId(Convert.toLong(smartParkUserConfig.getUserId()));
            smartParkUserTzVO.setUserType(1);
            smartParkUserTzVO.setPhoneNum(smartParkUserConfig.getPhoneNum());
            smartParkUserTzVO.setCharacteristic(smartParkUserConfig.getCharacteristic());
            smartParkUserTzVO.setUserName(smartParkUserConfig.getName());
            if (smartParkUserConfig.getAccessDateType() != null && smartParkUserConfig.getAccessDateType() == 1) {
                smartParkUserTzVO.setAccessStartDate(smartParkUserConfig.getAccessStartDate());
                smartParkUserTzVO.setAccessEndDate(smartParkUserConfig.getAccessEndDate());
            }

            if(StringUtils.isNotNull(smartParkUserConfig.getIsLeader()) && smartParkUserConfig.getIsLeader()==1){
                smartParkUserTzVO.setSort(Long.MAX_VALUE);
            }else {
                smartParkUserTzVO.setSort(0);
            }
            smartParkUserTzVO.setGmtModified(smartParkUserConfig.getGmtModified()==null?smartParkUserConfig.getGmtCreate():smartParkUserConfig.getGmtModified());
            smartParkUserTzVO.setIdPhoto(smartParkUserConfig.getPhoto());
            //下发设备
            sendMqtt(opt,device,smartParkUserTzVO,MqttTopicType.ACCESSCONTROL.getTopicCode());
            //门禁卡权限下发
        }else if(StringUtils.isNotEmpty(cardNum) && MqttOptType.ADDUSERCARD.getOptCode().equals(opt)){
            SmartParkUserCardVO smartParkUserCardVO = new SmartParkUserCardVO();
            //查询卡号
            if(smartParkUserConfig.getBeAccessCard() != 1){
                return;
            }
            smartParkUserCardVO.setUserId(smartParkUserConfig.getUserId());
            smartParkUserCardVO.setUserType(1);
            smartParkUserCardVO.setPhoneNum(smartParkUserConfig.getPhoneNum());
            smartParkUserCardVO.setUserName(smartParkUserConfig.getName());
            if (smartParkUserConfig.getAccessDateType() != null && smartParkUserConfig.getAccessDateType() == 1) {
                smartParkUserCardVO.setAccessStartDate(smartParkUserConfig.getAccessStartDate());
                smartParkUserCardVO.setAccessEndDate(smartParkUserConfig.getAccessEndDate());
            }
            smartParkUserCardVO.setGmtModified(smartParkUserConfig.getGmtModified() == null ? smartParkUserConfig.getGmtCreate() : smartParkUserConfig.getGmtModified());
            smartParkUserCardVO.setCardNum(cardNum);
            //下发设备
            sendMqtt(opt,device,smartParkUserCardVO,MqttTopicType.ACCESSCONTROL.getTopicCode());
        }else if(StringUtils.isNotEmpty(cardNum) && MqttOptType.ADDUSERCAR.getOptCode().equals(opt)){
            SmartParkUserCarVO smartParkUserCarVO = new SmartParkUserCarVO();
            //查询车牌
            smartParkUserCarVO.setUserId(Convert.toLong(smartParkUserConfig.getUserId()));
            smartParkUserCarVO.setUserType(1);
            smartParkUserCarVO.setPhoneNum(smartParkUserConfig.getPhoneNum());
            smartParkUserCarVO.setUserName(smartParkUserConfig.getName());
            if (smartParkUserConfig.getAccessDateType() != null && smartParkUserConfig.getAccessDateType() == 1) {
                smartParkUserCarVO.setAccessStartDate(smartParkUserConfig.getAccessStartDate().getTime());
                smartParkUserCarVO.setAccessEndDate(smartParkUserConfig.getAccessEndDate().getTime());
            }
            smartParkUserCarVO.setGmtModified(smartParkUserConfig.getGmtModified() == null ? smartParkUserConfig.getGmtCreate().getTime() : smartParkUserConfig.getGmtModified().getTime());
            smartParkUserCarVO.setCarNum(cardNum);
            //下发设备
            sendMqtt(opt,device,smartParkUserCarVO,MqttTopicType.CARBRAKE.getTopicCode());
        }
    }


    /**
     * 批量下发用户卡、车牌权限
     *
     * @param userId 用户id
     */
    @Override
    public void sendUserDeviceAuthBatch(String userId, Integer opt) throws Exception {
        //下发设备权限
        //查出用户已有设备
        //根据设备code查询所有绑定人
        List<String> devices = getDeviceByUserId(userId);
        if(StringUtils.isEmpty(devices)){
            return;
        }
        String device = ArrayUtil.join(devices.toArray(),",");
        //查询员工凭证
        SmartParkUserConfigExample smartParkUserConfigExample = new SmartParkUserConfigExample();
        SmartParkUserConfigExample.Criteria criteria = smartParkUserConfigExample.createCriteria();
        criteria.andIsDeletedEqualTo(0);
        criteria.andAccessStartDateLessThanOrEqualTo(new Date());
        criteria.andAccessEndDateGreaterThanOrEqualTo(new Date());
        criteria.andUserIdEqualTo(Convert.toLong(userId));
        List<SmartParkUserConfig> passRecords = smartParkUserConfigMapper.selectByExample(smartParkUserConfigExample);
        if (StringUtils.isEmpty(passRecords)) {
            return;
        }
        SmartParkUserConfig smartParkUserConfig = passRecords.get(0);
        if(MqttOptType.ADDUSERCARD.getOptCode().equals(opt)){
            if(smartParkUserConfig.getBeAccessCard() != 1){
                return;
            }
            SmartParkCardExample smartParkCardExample = new SmartParkCardExample();
            SmartParkCardExample.Criteria cardExampleCriteria = smartParkCardExample.createCriteria();
            cardExampleCriteria.andUserIdEqualTo(Convert.toLong(userId));
            cardExampleCriteria.andStatusEqualTo(1);
            cardExampleCriteria.andIsDeletedEqualTo(0);
            cardExampleCriteria.andTenantIdEqualTo(smartParkUserConfig.getTenantId());
            smartParkCardExample.setOrderByClause("gmt_create desc");
            List<SmartParkCard>  smartParkCards = smartParkCardMapper.selectByExample(smartParkCardExample);
            for(SmartParkCard card : smartParkCards){
                SmartParkUserCardVO smartParkUserCardVO = new SmartParkUserCardVO();
                //查询卡号
                smartParkUserCardVO.setUserId(smartParkUserConfig.getUserId());
                smartParkUserCardVO.setUserType(1);
                smartParkUserCardVO.setPhoneNum(smartParkUserConfig.getPhoneNum());
                smartParkUserCardVO.setUserName(smartParkUserConfig.getName());
                if (smartParkUserConfig.getAccessDateType() != null && smartParkUserConfig.getAccessDateType() == 1) {
                    smartParkUserCardVO.setAccessStartDate(smartParkUserConfig.getAccessStartDate());
                    smartParkUserCardVO.setAccessEndDate(smartParkUserConfig.getAccessEndDate());
                }
                smartParkUserCardVO.setGmtModified(smartParkUserConfig.getGmtModified() == null ? smartParkUserConfig.getGmtCreate() : smartParkUserConfig.getGmtModified());
                smartParkUserCardVO.setCardNum(card.getCardNum());
                sendMqtt(opt,device,smartParkUserCardVO,MqttTopicType.ACCESSCONTROL.getTopicCode());
            }
        }else if(MqttOptType.ADDUSERCAR.getOptCode().equals(opt)){
            //查询车牌
            SmartParkCarExample smartParkCarExample = new SmartParkCarExample();
            SmartParkCarExample.Criteria carExampleCriteria = smartParkCarExample.createCriteria();
            carExampleCriteria.andTenantIdEqualTo(smartParkUserConfig.getTenantId());
            carExampleCriteria.andIsDeletedEqualTo(0);
            carExampleCriteria.andUserIdEqualTo(Convert.toLong(userId));
            List<SmartParkCar> smartParkCars = smartParkCarMapper.selectByExample(smartParkCarExample);
            for(SmartParkCar car : smartParkCars){
                SmartParkUserCarVO smartParkUserCarVO = new SmartParkUserCarVO();
                smartParkUserCarVO.setUserId(Convert.toLong(smartParkUserConfig.getUserId()));
                smartParkUserCarVO.setUserType(1);
                smartParkUserCarVO.setPhoneNum(smartParkUserConfig.getPhoneNum());
                smartParkUserCarVO.setUserName(smartParkUserConfig.getName());
                if (smartParkUserConfig.getAccessDateType() != null && smartParkUserConfig.getAccessDateType() == 1) {
                    smartParkUserCarVO.setAccessStartDate(smartParkUserConfig.getAccessStartDate().getTime());
                    smartParkUserCarVO.setAccessEndDate(smartParkUserConfig.getAccessEndDate().getTime());
                }
                smartParkUserCarVO.setGmtModified(smartParkUserConfig.getGmtModified() == null ? smartParkUserConfig.getGmtCreate().getTime() : smartParkUserConfig.getGmtModified().getTime());
                smartParkUserCarVO.setCarNum(car.getLicensePlate());
                sendMqtt(opt,device,smartParkUserCarVO,MqttTopicType.CARBRAKE.getTopicCode());
            }
        }
    }

    /**
     * 下发删除设备权限
     * @param userId 用户id
     * @param opt
     * @param cardNum
     * @throws Exception
     */
    @Override
    public void sendUserDeviceDelAuth(String userId, Integer opt, String cardNum) throws Exception {
        //下发设备权限
        //查出用户已有设备
        //根据设备code查询所有绑定人
        List<String> devices = getDeviceByUserId(userId);
        if(StringUtils.isEmpty(devices)){
            return;
        }
        String device = ArrayUtil.join(devices.toArray(),",");
        Map<String, String> data = MapUtil.newHashMap();
        data.put("userId",userId);
        String topic = null;
        //卡删除
        if(StringUtils.isNotEmpty(cardNum) && MqttOptType.DELUSERCARD.getOptCode().equals(opt)){
            data.put("cardNum",cardNum);
            topic = MqttTopicType.ACCESSCONTROL.getTopicCode();
        }

        //车牌删除
        if(StringUtils.isNotEmpty(cardNum) && MqttOptType.DELUSERCAR.getOptCode().equals(opt)){
            data.put("carNum",cardNum);
            topic = MqttTopicType.CARBRAKE.getTopicCode();
        }

        //人脸删除
        if(MqttOptType.DELUSERFACE.getOptCode().equals(opt)){
            topic = MqttTopicType.ACCESSCONTROL.getTopicCode();
        }
        sendMqtt(opt,device,data,topic);
    }

    /**
     * 批量下发删除用户卡、车牌权限
     * @param userId 用户id
     * @param opt
     * @throws Exception
     */
    @Override
    public void sendUserDeviceDelAuthBatch(String userId, Integer opt ,String tenantId) throws Exception {
        //下发设备权限
        //查出用户已有设备
        //根据设备code查询所有绑定人
        List<String> devices = getDeviceByUserId(userId);
        if(StringUtils.isEmpty(devices)){
            return;
        }
        String device = ArrayUtil.join(devices.toArray(),",");
        //卡
        if(MqttOptType.ADDUSERCARD.getOptCode().equals(opt)){
            SmartParkCardExample smartParkCardExample = new SmartParkCardExample();
            SmartParkCardExample.Criteria cardExampleCriteria = smartParkCardExample.createCriteria();
            cardExampleCriteria.andUserIdEqualTo(Convert.toLong(userId));
            cardExampleCriteria.andStatusEqualTo(1);
            cardExampleCriteria.andIsDeletedEqualTo(0);
            cardExampleCriteria.andTenantIdEqualTo(tenantId);
            smartParkCardExample.setOrderByClause("gmt_create desc");
            List<SmartParkCard>  smartParkCards = smartParkCardMapper.selectByExample(smartParkCardExample);
            for(SmartParkCard card : smartParkCards){
                Map<String, String> data = MapUtil.newHashMap();
                data.put("userId",userId);
                data.put("cardNum",card.getCardNum());
                sendMqtt(opt,device,data,MqttTopicType.ACCESSCONTROL.getTopicCode());
            }
        }

        //车牌
        if(MqttOptType.DELUSERCAR.getOptCode().equals(opt)){
            SmartParkCarExample smartParkCarExample = new SmartParkCarExample();
            SmartParkCarExample.Criteria carExampleCriteria = smartParkCarExample.createCriteria();
            carExampleCriteria.andTenantIdEqualTo(tenantId);
            carExampleCriteria.andIsDeletedEqualTo(0);
            carExampleCriteria.andUserIdEqualTo(Convert.toLong(userId));
            List<SmartParkCar> smartParkCars = smartParkCarMapper.selectByExample(smartParkCarExample);
            for(SmartParkCar car : smartParkCars){
                Map<String, String> data = MapUtil.newHashMap();
                data.put("userId",userId);
                data.put("carNum",car.getLicensePlate());
                sendMqtt(opt,device,data, MqttTopicType.CARBRAKE.getTopicCode());
            }
        }

    }

    /**
     * 根据人员id查询权限设备
     * @param userId
     * @return
     * @throws Exception
     */
    public List<String> getDeviceByUserId(String userId) throws Exception {
        //查询人员机构
        SsoUserRpcQO ssoUserRpcQO = new SsoUserRpcQO();
        ssoUserRpcQO.setUserId(userId);
        ssoUserRpcQO.setNeedPage(false);
        CommonResponseBody<SsoUserDTO>  aclUserRpcServiceUser = aclUserRpcService.getUser(ssoUserRpcQO);
        if(!aclUserRpcServiceUser.isSuccess() || StringUtils.isNull(aclUserRpcServiceUser.getData())){
            return null;
        }
        SsoUserDTO ssoUserDTO = aclUserRpcServiceUser.getData();
        List<SsoUserDeptDTO> depts = ssoUserDTO.getDepts();
        if(StringUtils.isEmpty(depts)){
            return null;
        }
        List<String> deptIds= depts.stream().map(ssoUserDeptDTO->Convert.toStr(ssoUserDeptDTO.getDeptId())).distinct().collect(Collectors.toList());
        //通过机构查询授权区域
        //查询区域
        AclDeptDicQO aclDeptDicQO = new AclDeptDicQO();
        aclDeptDicQO.setDeptCodes(deptIds);
        aclDeptDicQO.setNeedPage(false);
        CommonResponseBody<List<AclDeptDicDTO>> aclDeptDicRpcServiceList= aclDeptDicRpcService.getList(aclDeptDicQO);
        if(!aclDeptDicRpcServiceList.isSuccess() || StringUtils.isEmpty(aclDeptDicRpcServiceList.getData())){
            return null;
        }
        List<AclDeptDicDTO> aclDeptDicDTOS = aclDeptDicRpcServiceList.getData();
        List<String> areas = aclDeptDicDTOS.stream().map(AclDeptDicDTO::getDicCode).distinct().collect(Collectors.toList());
        //通过区域查询区域设备
        SmartDeviceRpcQO smartDeviceRpcQO = new SmartDeviceRpcQO();
        smartDeviceRpcQO.setDeviceAreaCodes(areas);
        smartDeviceRpcQO.setTenantId(ssoUserDTO.getTenantId());
        smartDeviceRpcQO.setOrgCode(deptIds.get(0));
        smartDeviceRpcQO.setNeedPage(false);
        CommonResponseBody<List<SmartDeviceDTO>> smartParkDeviceRpcServiceDevices = smartParkDeviceRpcService.getDevices(smartDeviceRpcQO);
        if(!smartParkDeviceRpcServiceDevices.isSuccess() || StringUtils.isEmpty(smartParkDeviceRpcServiceDevices.getData())){
            return null;
        }
        List<String> smartDeviceDTOS = smartParkDeviceRpcServiceDevices.getData().stream()
                .map(SmartDeviceDTO :: getDeviceCode).distinct().collect(Collectors.toList());
        return smartDeviceDTOS;
    }



    /**
     * 根据设备code查询人员信息
     * @param deviceCode
     * @return
     * @throws Exception
     */
    @Override
    public  List<String> getUserIdByDeviceCode(String deviceCode,String orgCode,String tenantId) throws Exception {
        //查询设备区域
        SmartDeviceRpcQO smartDeviceRpcQO = new SmartDeviceRpcQO();
        smartDeviceRpcQO.setDeviceCode(deviceCode);
        smartDeviceRpcQO.setTenantId(tenantId);
        smartDeviceRpcQO.setOrgCode(orgCode);
        smartDeviceRpcQO.setNeedPage(false);
        CommonResponseBody<List<SmartDeviceDTO>> smartParkDeviceRpcServiceDevices = smartParkDeviceRpcService.getDevices(smartDeviceRpcQO);
        if(!smartParkDeviceRpcServiceDevices.isSuccess() || StringUtils.isEmpty(smartParkDeviceRpcServiceDevices.getData())){
            return null;
        }
        SmartDeviceDTO smartDeviceDTO = smartParkDeviceRpcServiceDevices.getData().get(0);

        //查询区域授权机构
        AclDeptDicQO aclDeptDicQO = new AclDeptDicQO();
        aclDeptDicQO.setDicCode(smartDeviceDTO.getDeviceAreaCode());
        aclDeptDicQO.setNeedPage(false);
        CommonResponseBody<List<AclDeptDicDTO>> aclDeptDicRpcServiceList= aclDeptDicRpcService.getList(aclDeptDicQO);
        if(!aclDeptDicRpcServiceList.isSuccess() || StringUtils.isEmpty(aclDeptDicRpcServiceList.getData())){
            return null;
        }
        List<AclDeptDicDTO> aclDeptDicDTOS = aclDeptDicRpcServiceList.getData();
        List<Long> deptIds = aclDeptDicDTOS.stream().map(aclDeptDicDTO->Convert.toLong(aclDeptDicDTO.getDeptCode())).collect(Collectors.toList());
        SsoDeptRpcQO ssoDeptRpcQO = new SsoDeptRpcQO();
        ssoDeptRpcQO.setDeptIds(deptIds);
        ssoDeptRpcQO.setNeedPage(false);
        CommonResponseBody<List<SsoUserDeptDTO>>aclUserRpcServiceUserByDeptId = aclUserRpcService.getUserByDeptId(ssoDeptRpcQO);
        if(!aclUserRpcServiceUserByDeptId.isSuccess() || StringUtils.isEmpty(aclUserRpcServiceUserByDeptId.getData())){
            return null;
        }
        return aclUserRpcServiceUserByDeptId.getData().stream().map(ssoUserDeptDTO->Convert.toStr(ssoUserDeptDTO.getUserId())).distinct().collect(Collectors.toList());
    }

    /**
     * 查询人员是否有设备权限
     * @param deviceCode
     * @param userId
     * @return
     * @throws Exception
     */
    @Override
    public  Boolean authUserIdandDeviceCode(String deviceCode,String userId,String orgCode,String tenantId) throws Exception {
        //查询设备区域
        SmartDeviceRpcQO smartDeviceRpcQO = new SmartDeviceRpcQO();
        smartDeviceRpcQO.setDeviceCode(deviceCode);
        smartDeviceRpcQO.setTenantId(tenantId);
        smartDeviceRpcQO.setOrgCode(orgCode);
        smartDeviceRpcQO.setNeedPage(false);
        CommonResponseBody<List<SmartDeviceDTO>> smartParkDeviceRpcServiceDevices = smartParkDeviceRpcService.getDevices(smartDeviceRpcQO);
        if(!smartParkDeviceRpcServiceDevices.isSuccess() || StringUtils.isEmpty(smartParkDeviceRpcServiceDevices.getData())){
            return false;
        }
        SmartDeviceDTO smartDeviceDTO = smartParkDeviceRpcServiceDevices.getData().get(0);

        //查询区域授权机构
        AclDeptDicQO aclDeptDicQO = new AclDeptDicQO();
        aclDeptDicQO.setDicCode(smartDeviceDTO.getDeviceAreaCode());
        aclDeptDicQO.setNeedPage(false);
        CommonResponseBody<List<AclDeptDicDTO>> aclDeptDicRpcServiceList= aclDeptDicRpcService.getList(aclDeptDicQO);
        if(!aclDeptDicRpcServiceList.isSuccess() || StringUtils.isEmpty(aclDeptDicRpcServiceList.getData())){
            return false;
        }
        List<AclDeptDicDTO> aclDeptDicDTOS = aclDeptDicRpcServiceList.getData();
        List<Long> deptIds = aclDeptDicDTOS.stream().map(aclDeptDicDTO->Convert.toLong(aclDeptDicDTO.getDeptCode())).distinct().collect(Collectors.toList());
        SsoDeptRpcQO ssoDeptRpcQO = new SsoDeptRpcQO();
        ssoDeptRpcQO.setDeptIds(deptIds);
        ssoDeptRpcQO.setUserId(userId);
        ssoDeptRpcQO.setNeedPage(false);
        CommonResponseBody<List<SsoUserDeptDTO>>aclUserRpcServiceUserByDeptId = aclUserRpcService.getUserByDeptId(ssoDeptRpcQO);
        if(aclUserRpcServiceUserByDeptId.isSuccess() && StringUtils.isNotEmpty(aclUserRpcServiceUserByDeptId.getData())){
            return true;
        }
        return false;
    }

    /**
     * mqtt 数据下发
     * @param opt  操作类型
     * @param device  设备code  多个逗号分隔
     * @param data  下发数据
     */
    @Override
    public void sendMqtt(int opt,String device,Object data,String topic){
        //下发设备
        MqttMessageVo mqttMessageVo = new MqttMessageVo();
        mqttMessageVo.setData(JSON.toJSONString(data));
        mqttMessageVo.setDeviceCode(device);
        mqttMessageVo.setType(opt);
        if(StringUtils.isNotEmpty(topic)){
            mqttService.sendToMqtt(JSON.toJSONString(mqttMessageVo),topic);
            mqtt.info("！！！！！！！！！！！！！推送数据完成:{},topic:{}",mqttMessageVo.toString(),topic);
        }else {
            mqtt.info("！！！！！！！！！！！！！推送数据失败:{},topic:{}",mqttMessageVo.toString(),topic);
        }
    }

}
